package data_generator.xml;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;

import data_generator.entity.NodeLocation;
import data_generator.entity.SensorNode;
import data_generator.entity.Temperature;

public final class DataCreator {
	private DataCreator() {

	}
	
	final static String currentDay="2013-05-20";
    final static int MINUTES=1440;
    
	public static ArrayList<SensorNode> createData() {
		ArrayList<SensorNode> snList = new ArrayList<SensorNode>();
        		
		snList.add(generateNewNode("1", "Node1", new NodeLocation(100, 100, 1), currentDay, 25, 5));
		snList.add(generateNewNode("2", "Node2", new NodeLocation(210, 250, 1), currentDay, 30, 10));
		snList.add(generateNewNode("3", "Node3", new NodeLocation(300, 320, 1), currentDay, 35, 15));
		snList.add(generateNewNode("4", "Node4", new NodeLocation(570, 610, 1), currentDay, 20, 0));

		
		snList.add(generateNewNode("5", "Node5", new NodeLocation(120, 80, 2), currentDay, 25, 10));		
		snList.add(generateNewNode("6", "Node6", new NodeLocation(230, 130, 2), currentDay, 35, 15));
		snList.add(generateNewNode("7", "Node7", new NodeLocation(360, 430, 2), currentDay, 30, 12));
		snList.add(generateNewNode("8", "Node8", new NodeLocation(530, 670, 2), currentDay, 25, 5));
		
		return snList;
	}

	private static float generateTemperature(float currenttime,
			float upperbound, float lowerbound) {
		/*
		 * The temperature reaches its highest value in 14H and lowest value in
		 * 2H
		 */
		float tempValue = (float) ((upperbound-lowerbound)/2
				* Math.sin((currenttime - 8) * Math.PI / 12) + (upperbound+lowerbound)/2);
		Random rdm = new Random(System.currentTimeMillis());
		/* Real value of temperature changes within +- 0.5 centigrade randomly */
		float tmpt = (float) ((rdm.nextFloat() - 0.5) + tempValue);
		return tmpt;
	}

	private static SensorNode generateNewNode(String id, String name,
			NodeLocation location, String dayStr, int highestTemperature, int lowestTemperature) {
		SensorNode newNode = new SensorNode();
        newNode.setId(id);
        newNode.setName(name);
        newNode.setLocation(location);
        ArrayList<Temperature> tmptList=new ArrayList<Temperature>();
        for(int i=0;i<MINUTES;i++){
        	int hour=i/60;
        	int minute=i%60;
        	String timeStr=dayStr+" "+String.valueOf(hour)+":"+String.valueOf(minute)+":00";
        	float tmptFlt=generateTemperature(((float)i/60), highestTemperature, lowestTemperature);
        	DecimalFormat df=new DecimalFormat("##.00");
        	tmptFlt=Float.parseFloat(df.format(tmptFlt));
        	Temperature tmpt=new Temperature(timeStr, tmptFlt);
        	 
        	tmptList.add(tmpt);
        }
        newNode.setTemperatureList(tmptList);
		return newNode;
	}
}
